package com.example.demo.service.Impl;

import com.example.demo.entity.Application;
import com.example.demo.entity.Licence;
import com.example.demo.mapper.ApplicationMapper;
import com.example.demo.mapper.LicenceMapper;
import com.example.demo.service.LicenceService;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
@RequiredArgsConstructor
@Transactional
public class LicenceImpl implements LicenceService {

    private final LicenceMapper licenceMapper;
    private final ApplicationMapper applicationMapper;
    @Override
    public List<Licence> findAllLicence() {
        return this.licenceMapper.selectList(null);
    }

    @Override
    public List<Map.Entry<String, Double>> findHotLicence() {
        Map<String,Double> result = new HashMap<>();
        List<Application> applications = applicationMapper.findAllApplication();
        for(int i = 0; i<applications.size();i++){
            Long j=applications.get(i).getLicenceId();
            if (j == 1) {
                if(result.containsKey("A1")){
                    result.replace("A1",result.get("A1")+1);
                }else {
                    result.put("A1", 1.0);
                }
            }
            else if(j==2){
                if(result.containsKey("A2")){
                    result.replace("A2",result.get("A2")+1);
                }else {
                    result.put("A2", 1.0);
                }
            }
            else if(j==3){
                if(result.containsKey("B1")){
                    result.replace("B1",result.get("B1")+1);
                }else {
                    result.put("B1", 1.0);
                }
            }
            else if(j==4){
                if(result.containsKey("B2")){
                    result.replace("B2",result.get("B2")+1);
                }else {
                    result.put("B2", 1.0);
                }
            }
            else if(j==5){
                if(result.containsKey("C1")){
                    result.replace("C1",result.get("C1")+1);
                }else {
                    result.put("C1", 1.0);
                }
            }
            else if(j==6){
                if(result.containsKey("C2")){
                    result.replace("C2",result.get("C2")+1);
                }else {
                    result.put("C2", 1.0);
                }
            }
            else if(j==7){
                if(result.containsKey("D")){
                    result.replace("D",result.get("D")+1);
                }else {
                    result.put("D", 1.0);
                }
            }
            else if(j==8){
                if(result.containsKey("E")){
                    result.replace("E",result.get("E")+1);
                }else {
                    result.put("E", 1.0);
                }
            }
            else if(j==9){
                if(result.containsKey("E")){
                    result.replace("E",result.get("E")+1);
                }else {
                    result.put("E", 1.0);
                }
            }
        }
        List<Map.Entry<String, Double>> list = new ArrayList<>(result.entrySet());
        //list.sort()
        list.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue()));
        Double temp = list.get(0).getValue();
        for(int i = 0;i<list.size();i++){
            list.get(i).setValue(list.get(i).getValue()/temp*5);
        }
        return list;
    }


    @Override
    public Licence addLicence(Licence licence) {
        int result = licenceMapper.insert(licence);
        if(result>0){
            return licence;
        }
        else {
            throw new RuntimeException("Failed to add Licence");
        }
    }

    @Override
    public Long deleteLicence(Long licenceId) {
//        QueryWrapper queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("licence_id",licenceId);
        int result = licenceMapper.deleteById(licenceId);
        if(result>0){
            return licenceId;
        }
        else {
            throw new RuntimeException("Failed to delete Licence");
        }
    }

    @Override
    public Licence updateLicence(Licence licence) {
        int result = licenceMapper.updateById(licence);
        if(result>0){
            return licence;
        }
        else {
            throw new RuntimeException("Failed to update Licence");
        }
    }


}
